package com.siwei.mes.controller;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.Project;
import com.siwei.mes.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 砼行工程表(Project)表控制层
 *
 * @author makejava
 * @since 2023-11-30 15:45:00
 */
@Api(value = "砼行工程表")
@RestController
@RequestMapping("project")
public class ProjectController {
    @Resource
    private ProjectService projectService;

    @ApiOperation(value = "新增砼行工程表")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) Project entity) {
        return Json.succ().data(ApiResult.addColumn("id", projectService.create(entity)));
    }

    @ApiOperation(value = "修改砼行工程表")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) Project entity) {
        projectService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除砼行工程表")
    @PostMapping("/delete")
    public Json delete(@RequestBody Project entity) {
        projectService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取砼行工程表")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<Project, Project> query) {
        return Json.succ().data(projectService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取砼行工程表")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(projectService.loadById(id));
    }

    @ApiOperation(value = "获取所有砼行工程表")
    @PostMapping("getAll")
    public Json getAll(@RequestBody Project entity) {
        return Json.succ().data(ApiResult.addList(projectService.getAll(entity)));
    }

    @ApiOperation("修改地址")
    @PostMapping("updateAddress")
    public Json updateAddress(@RequestBody Project entity) {
        assertTrue(StringUtils.isBlank(entity.getProjectAdd()), "项目地址为空");
        assertTrue(entity.getLongitude() == null, "经度为空");
        assertTrue(entity.getLatitude() == null, "纬度为空");
        Project project = projectService.loadById(entity.getId());
        assertTrue(project != null, "项目不存在");
        projectService.update(entity);
        // TODO: 2023/11/30 同步砼行
        return Json.succ().data(new ApiResult());
    }

}
